package cn.com.headfree.workflowant.controller;

import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.commworkflow.dao.IWorkFlowApproveDao;
import cn.com.headfree.commworkflow.model.TWorkFlowApprove;
import cn.com.headfree.entity.*;
import cn.com.headfree.workflowant.dao.IBusinessWorkflowApproveBindDao;
import cn.com.headfree.workflowant.dao.IWorkFlowAntDao;
import cn.com.headfree.workflowant.dto.ApproveWorkFlowDto;
import cn.com.headfree.workflowant.service.IWorkFlowAntService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2021/6/30
 **/
@Controller
@RequestMapping(value = "/flowtest", produces = "application/json;charset=UTF-8")
public class TestController {

    @Autowired
    private IWorkFlowAntDao workFlowAntDao;

    @Autowired
    private IBusinessWorkflowApproveBindDao businessWorkflowApproveBindDao;

    /**
     * 中间表
     */
    @RequestMapping("/bind")
    @ResponseBody
    public ResultData pointsalary(String orgId, String mouldId){
        ResultData resultData = new ResultData();
        try {
            List<TMouldInfo> moduleInfoList = workFlowAntDao.getMouldInfoList(mouldId);
            for (TMouldInfo module : moduleInfoList) {
                TBusinessWorkflowBind bind = workFlowAntDao.getWorkFlowBind(new TBusinessWorkflowBind(module.getId(), orgId));
                if (bind == null) {
                    continue;
                }
                List<ApproveWorkFlowDto> approveWorkFlowDtoList = workFlowAntDao.getBusinessAllData(module.getTabName(), bind.getFlowId(), orgId);
                if (CollectionUtils.isNotEmpty(approveWorkFlowDtoList)) {

                    approveWorkFlowDtoList.stream().forEach(item -> {
                        List<TOrgDept> deptList = workFlowAntDao.getDeptByErpNo(item.getErpNo());
                        if ((null != deptList) && (0 < deptList.size())) {
                            List<TBusinessWorkflowApproveBind> bindList = businessWorkflowApproveBindDao.selectBusinessWorkflowApproveBindByApproveId(item.getApproveId());
                            if (CollectionUtils.isEmpty(bindList)) {
                                TBusinessWorkflowApproveBind businessWorkflowApproveBind = new TBusinessWorkflowApproveBind();
                                businessWorkflowApproveBind.setApproveId(item.getApproveId());
                                businessWorkflowApproveBind.setOrgId(orgId);
                                businessWorkflowApproveBind.setDeptId(deptList.get(0).getId());
                                businessWorkflowApproveBind.setUserName(item.getUserName());
                                businessWorkflowApproveBind.setErpNo(item.getErpNo());
                                businessWorkflowApproveBind.setBusinessTypeId(module.getId());
                                businessWorkflowApproveBind.setHandleTime(item.getHandleTime());
                                businessWorkflowApproveBind.setDealTime(item.getDealTime());
                                businessWorkflowApproveBind.setBusinessId(item.getBusinessId());
                                businessWorkflowApproveBind.setFlowStatus(item.getFlowStatus());
                                businessWorkflowApproveBindDao.save(businessWorkflowApproveBind);
                            }
                        }
                    });
                }
            }
        }catch(Exception e){
            resultData.setSuccess(false);
            resultData.setErrorMsg(e.getMessage());
        }
        return resultData;
    }
}
